package com.ecloud.gardensterminal.controller;

import com.ecloud.gardensterminal.common.JsonResult;
import com.ecloud.gardensterminal.controller.dto.GardensAreaDto;
import com.ecloud.gardensterminal.controller.dto.GardensAreaMappingDto;
import com.ecloud.gardensterminal.controller.vo.GardensAreaDeviceVo;
import com.ecloud.gardensterminal.domain.GardensArea;
import com.ecloud.gardensterminal.server.GardensAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Squbi
 */
@RestController
public class GardensAreaController {
    @Autowired
    private GardensAreaService areaService;

    @PostMapping("/area")
    public JsonResult newArea(@RequestBody GardensAreaDto dto) {
        areaService.newArea(dto.getAreaName(), dto.getAreaDescription());
        return JsonResult.success();
    }

    @DeleteMapping("/area")
    public JsonResult deleteArea(int areaId) {
        areaService.deleteArea(areaId);
        return JsonResult.success();
    }

    @GetMapping("/area")
    public JsonResult selectArea() {
        List<GardensArea> gardensAreas = areaService.selectAllArea();
        return JsonResult.success(gardensAreas);
    }

    @PutMapping("/area")
    public JsonResult updateArea(@RequestBody GardensArea area) {
        areaService.updateArea(area);
        return JsonResult.success();
    }

    @PostMapping("/area/mapping")
    public JsonResult newMapping(@RequestBody GardensAreaMappingDto dto) {
        areaService.newMapping(dto.getAreaId(), dto.getDeviceId());
        return JsonResult.success();
    }

    @DeleteMapping("/area/mapping")
    public JsonResult deleteMapping(@RequestBody GardensAreaMappingDto dto) {
        areaService.deleteMapping(dto.getAreaId(), dto.getDeviceId());
        return JsonResult.success();
    }

    @GetMapping("/area/mapping")
    public JsonResult selectMapping(int areaId) {
        GardensAreaDeviceVo gardensAreaDeviceVo = areaService.selectAreaMapping(areaId);
        return JsonResult.success(gardensAreaDeviceVo);
    }
}
